﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace workshop3
{
    public partial class Formstudentinfor : Form
    {
        string filepath, realfilename;
        DataTable dt = new DataTable();//create datatable to store the course information
        DataColumn dc1 = new DataColumn("coursename");
        DataColumn dc4 = new DataColumn("courseid");
        public Formstudentinfor()
        {
            InitializeComponent();
            //Font = SystemFonts.MenuFont;
            //AutoReSizeForm.SetFormSize(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Formstudentinfor_Load(object sender, EventArgs e)
        {
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc4);
            XmlDocument xml = new XmlDocument();
            xml.Load("course.xml");
            string path = "//courses/course";
            XmlNodeList nodelist = xml.SelectNodes(path);
            for(int i = 0; i < nodelist.Count; i++)//load all the existing course
            {
                string course = nodelist[i].ChildNodes[0].InnerText + " "+nodelist[i].ChildNodes[1].InnerText + "("+nodelist[i].ChildNodes[2].InnerText+")";
                string courseid = nodelist[i].Attributes[0].Value;
                dt.Rows.Add(course,courseid);
                
            }
            cbcourse.DataSource = dt;//set the source to the combobox list
            cbcourse.DisplayMember = dt.Columns[0].ToString();
            cbcourse.ValueMember = dt.Columns[1].ToString();


        }

        private void buttonselect_Click(object sender, EventArgs e)
        {
            //lblcourseid.Text = cbcourse.SelectedValue.ToString();
            OpenFileDialog fileDialog = new OpenFileDialog();//open the filedialog
            fileDialog.Multiselect = true;
            fileDialog.Title = "Select File";
            fileDialog.Filter = "All file(*xls*)|*.xls*"; //set the file type
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                filepath = fileDialog.FileName;//return the file path    
                int index = filepath.LastIndexOf('\\');
                String filename = filepath.Substring(index + 1);
                int index2 = filename.IndexOf('.');
                if (filename.Substring(index2) == ".xls" || filename.Substring(index2) == ".xlsx")//check if the selected file is the excel file or not
                {
                    realfilename = filename.Substring(0, index2);//extract the file name from the file path
                    txtfilepath.Text = realfilename + "_" + cbcourse.Text.ToString();//extract the file path
                }
                else
                {
                    MessageBox.Show("Please choose the excel file!");
                }
            }
        }

        private void lblcoursename_Click(object sender, EventArgs e)
        {

        }

        private void cbcourse_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnimport_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtfilepath.Text))
            {
                ClassExcelToXml.ExcelToXml(filepath, txtfilepath.Text);//load the information into xml file

                if (File.Exists(txtfilepath.Text + ".xml"))
                {
                    addstudent(txtfilepath.Text, cbcourse.SelectedValue.ToString());
                    //MessageBox.Show("Import success!");
                }
                else
                {
                    MessageBox.Show("Fail!");
                }
            }
            else
            {
                MessageBox.Show("This filename has been exist!");
            }
        }

        private void addstudent(string filename,string courseid)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("course.xml");
            string path = "//courses/course[@id="+courseid+"]";
            XmlNode node = xml.SelectSingleNode(path);
            XmlElement xe = xml.CreateElement("studentfile");
            xe.InnerText = filename;
            node.AppendChild(xe);
            xml.Save("course.xml");
        }

        private void btnUser_Click(object sender, EventArgs e)//jump to the management page
        {
            Formmanagement user = new Formmanagement();
            user.Show();
            this.Hide();

        }

        private void btn_user_Click(object sender, EventArgs e)
        {
            Formmanagement user = new Formmanagement();
            user.Show();
            this.Hide();
        }

        private void txtfilepath_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
